package cn.yunyichina.provider.hymed.service.impl;

import cn.yunyichina.provider.hymed.constant.NeusoftFunctionId;
import cn.yunyichina.provider.hymed.constant.ResponseEnum;
import cn.yunyichina.provider.hymed.model.bo.MedicalCardQueryRequestBO;
import cn.yunyichina.provider.hymed.model.bo.MedicalCardQueryResponseBO;
import cn.yunyichina.provider.hymed.model.domain.HymedPaymentSettingDO;
import cn.yunyichina.provider.hymed.model.vo.BaseRequestVO;
import cn.yunyichina.provider.hymed.service.MedicareService;
import cn.yunyichina.provider.hymed.service.neusoft.NeusoftBaseService;
import cn.yunyichina.provider.hymed.service.neusoft.NeusoftService;
import cn.yunyichina.provider.iface.entity.common.ResCommon;
import cn.yunyichina.provider.iface.entity.dto.hymed.MedicalCardQueryRequestDTO;
import cn.yunyichina.provider.iface.entity.dto.hymed.MedicalCardQueryResponseDTO;
import cn.yunyichina.utils.dozer.Dozer;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Lullaby on 2017/10/26
 */
@Service
public class MedicareServiceImpl extends NeusoftBaseService implements MedicareService {

    private static final Logger logger = LoggerFactory.getLogger(MedicareServiceImpl.class);

    private static final String MEDICARE_CARD_BIND = "1";

    private final NeusoftService neusoftService;

    @Autowired
    public MedicareServiceImpl(NeusoftService neusoftService) {
        this.neusoftService = neusoftService;
    }

    @Override
    public ResCommon<MedicalCardQueryResponseDTO> queryMedicareCard(MedicalCardQueryRequestDTO requestDTO) {
        logger.info("MedicareServiceImpl.queryMedicareCard.requestDTO -> {}", JSON.toJSONString(requestDTO));
        ResCommon<MedicalCardQueryResponseDTO> response = new ResCommon<>();

        try {
            MedicalCardQueryRequestBO requestBO = new MedicalCardQueryRequestBO();
            requestBO.setFunction_id(NeusoftFunctionId.QUERY_MEDICAL_CARD);

            HymedPaymentSettingDO hymedPaymentSettingDO=getNeusoftSessionId(Dozer.convert(requestDTO, BaseRequestVO.class));
            requestBO.setSession_id(hymedPaymentSettingDO.getSessionId());
            requestBO.setPrivateKey(hymedPaymentSettingDO.getPrivateKey());
            requestBO.setPublicKey(hymedPaymentSettingDO.getPublicKey());
            requestBO.setPortalLoginId(requestDTO.getAccount());
            logger.info("MedicareServiceImpl.queryMedicareCard.requestBO -> {}", JSON.toJSONString(requestBO));

            MedicalCardQueryResponseBO responseBO = neusoftService.doExecute(requestBO, MedicalCardQueryResponseBO.class);
            logger.info("MedicareServiceImpl.queryMedicareCard.responseBO -> {}", JSON.toJSONString(responseBO));

            MedicalCardQueryResponseDTO responseDTO = Dozer.convert(responseBO, MedicalCardQueryResponseDTO.class);
            if (responseBO != null && responseBO.getBizSuccess() && MEDICARE_CARD_BIND.equals(responseBO.getBinding_id())) {
                return response.buildResult(responseDTO);
            } else {
                return response.buildResultCodeAndMessageAndResult(
                        ResponseEnum.MEDICAL_CARD_QUERY_FAIL.getCode(),
                        ResponseEnum.MEDICAL_CARD_QUERY_FAIL.getMessage(),
                        responseDTO
                );
            }
        } catch (Exception e) {
            logger.error("MedicareServiceImpl.queryMedicareCard.Exception", e);
            return response.buildResultCodeAndMessage(
                    ResponseEnum.MEDICAL_CARD_QUERY_EXCEPTION.getCode(),
                    ResponseEnum.MEDICAL_CARD_QUERY_EXCEPTION.getMessage()
            );
        }
    }

}
